package com.es.elasticsearch.controller;



import com.es.elasticsearch.model.item.AuthUserRecords;
import com.es.elasticsearch.model.item.PlanResponseInfo;
import com.es.elasticsearch.service.IPlanManagementService;
import com.es.elasticsearch.util.ParamException;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/planManagement")
@Slf4j
public class PlanManagementController {
    @Resource
    private IPlanManagementService planManagementService;

    /**
     * 新增预案
     *
     * @param request
     * @param planName
     * @param type
     * @param beginTime
     * @param endTime
     * @param authUserRecords
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addPlan", method = RequestMethod.POST)

    public PlanResponseInfo addPlan(HttpServletRequest request,
                                    @RequestParam(value = "planName", required = true) String planName,
                                    @RequestParam(value = "type", required = true) Integer type,
                                    @RequestParam(value = "beginTime", required = false)
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                                    @RequestParam(value = "endTime", required = false)
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                    @RequestParam(value = "authUserRecords", required = false) String authUserRecords) {
        PlanResponseInfo responseInfo = new PlanResponseInfo();
        responseInfo.setCode(200);
        responseInfo.setMessage("添加成功");
        Map<String, Object> paramMap = new HashMap<>();
        // 先判断type是否是定时预案类型，是的话，要确保开始时间和结束时间不能为空
        if (type == 2) {
            if (beginTime == null) {
                responseInfo.setMessage("定时预案的开始时间不能为空");
                throw new ParamException(responseInfo.getMessage());
            } else {
                paramMap.put("beginTime", beginTime);
            }
            if (endTime == null) {
                responseInfo.setMessage("定时预案的开始时间不能为空");
                throw new ParamException(responseInfo.getMessage());
            } else {
                paramMap.put("endTime", endTime);
            }
        }
        paramMap.put("name", planName);
        paramMap.put("type", type);
        paramMap.put("authUserRecords", authUserRecords);

        try {
            planManagementService.addPlan(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            responseInfo.setCode(500);
            responseInfo.setMessage("添加异常");
        }
        return responseInfo;
    }


    //删除预案
    @RequestMapping(value = "/deletePlan",method = RequestMethod.GET)
    @ResponseBody
    public Object deletePlan(@RequestParam(value = "id" ) Integer id){
        PlanResponseInfo responseInfo = new PlanResponseInfo();
        responseInfo.setCode(200);
        responseInfo.setMessage("删除成功");
        try {
            planManagementService.deletePlan(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            responseInfo.setCode(500);
            responseInfo.setMessage("删除异常");
        }
        return responseInfo;
    }





    //5查询授权用户记录列表
    @RequestMapping(value = "/getRecordListByPlanId",method = RequestMethod.GET)
    @ResponseBody
    public Object getRecordListByPlanId(@RequestParam(value = "planId" ) Integer planId){
        PlanResponseInfo responseInfo = new PlanResponseInfo();
        responseInfo.setCode(200);
        responseInfo.setMessage("查询成功");
        try {
            AuthUserRecords recordListByPlanId = planManagementService.getRecordListByPlanId(planId);
            responseInfo.setData(recordListByPlanId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            responseInfo.setCode(500);
            responseInfo.setMessage("查询异常");
        }
        return responseInfo;
    }



    //修改预案信息
    @RequestMapping(value = "/updatePlan",method = RequestMethod.POST)
    @ResponseBody
    public Object updatePlan(
            @RequestParam(value = "planId", required = true) Integer planId,
            @RequestParam(value = "planName", required = true) String planName,
            @RequestParam(value = "type", required = true) Integer type,
            @RequestParam(value = "beginTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(value = "authUserRecords", required = false) String authUserRecords
    ){
        PlanResponseInfo responseInfo = new PlanResponseInfo();
        responseInfo.setCode(200);
        responseInfo.setMessage("添加成功");
        Map<String, Object> paramMap = new HashMap<>();
        // 先判断type是否是定时预案类型，是的话，要确保开始时间和结束时间不能为空
        if (type == 2) {
            if (beginTime == null) {
                responseInfo.setMessage("定时预案的开始时间不能为空");
                throw new ParamException(responseInfo.getMessage());
            } else {
                paramMap.put("beginTime", beginTime);
            }
            if (endTime == null) {
                responseInfo.setMessage("定时预案的开始时间不能为空");
                throw new ParamException(responseInfo.getMessage());
            } else {
                paramMap.put("endTime", endTime);
            }
        }
        paramMap.put("name", planName);
        paramMap.put("planId", planId);
        paramMap.put("type", type);
        paramMap.put("authUserRecords", authUserRecords);

        try {
            planManagementService.updatePlan(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            responseInfo.setCode(500);
            responseInfo.setMessage("更新异常");
        }
        return responseInfo;
    }
}
